package com.clstu.qqclient.qqframe;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 该类用于显示在线用户列表
 */
public class OnlineListFrame extends JFrame implements ActionListener{

    JButton btAck;//退出键
    JTextArea areaMsg ;//显示聊天记录的对话框
    JPanel sendPanel;//创建面板,用于放置退出键
    JScrollPane msgPane;//创建带滚动条的面板
    private String userID;
    public OnlineListFrame(String userID, String users) {
        super("用户"+userID+"正在查看在线列表");
        this.userID = userID;
        areaMsg=new JTextArea (10,30); //创建文本域以显示用户信息
        msgPane=new JScrollPane(areaMsg);           //创建带滚动条的面板
        areaMsg.setEditable (false) ;             //让文本域不可编辑
        sendPanel=new JPanel();                 //创建面板,用于放置组件
        btAck = new JButton("退出");
        btAck.addActionListener(this);//加入事件监听器

        sendPanel.add(btAck) ;//添加按钮到面板，到此发送留言面板组合完毕

        this.add (msgPane, BorderLayout.CENTER);//将带滚动条的文本域添加到主窗体，位置为中央
        this.add (sendPanel,BorderLayout.SOUTH);//将发送留言面板添加到主窗体，位置为下方

        this.setLocation(500,300);
        this.setSize (500,200) ;
        this.setVisible (true) ;
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                new MainMenu(userID);
            }
        });

        //显示用户列表
        String[] s = users.split(" ");
        for (int i = 0; i < s.length; i++) {
            areaMsg.append("用户"+(i+1)+":  "+s[i]+ "\n");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        this.dispose();
        new MainMenu(userID);
    }
}
